package Sequence;

import java.util.Arrays;

public class Test1005 {
}
class Solution1005_1 {
    public int largestSumAfterKNegations(int[] nums, int k) {
        int ans = 0,min = Integer.MAX_VALUE;
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0 && k > 0) {
                nums[i] = -nums[i];
                k--;
            }
            ans += nums[i];
            min = Math.min(min, nums[i]);
        }
        if (k > 0) {
            if (k % 2 == 1) {
                ans -= min * 2;
            }
        }
        return ans;
    }
}
